// pages/addaddress/addaddress.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    price_name: '请选择位置信息',
    city_name: '',
    area_name: '',
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value_sel: [0, 0, 0],//对应数组下标
    province_s: [],
    city_s: [],
    device: [],
    area_s: [],
    areaInfo_name: [],//选中地区名称
    area_id_Info: [],//选中的地区id信息
    village_name:'',
    detailed_address:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var that = this;
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    that.animation = animation;
    that.getProvince();//获取省份信息
  },
  add_village(){
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/addVillage',
      method: "POST",
      load: true,
      data: { 
        province_name: that.data.price_name,
        city_name: that.data.city_name,
        area_name: that.data.area_name,
        province: that.data.province,
        city: that.data.city,
        area: that.data.area,
        village_name: that.data.village_name,
        address: that.data.detailed_address
        },
      success: function (res) {
        if (res.data.code == 1000) {

          app.showToast(res.data.message, "none", 2000, function () {
            wx.navigateTo({
              url: '../addresslist/addresslist',
            })
           });
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
        }
      }
    })
  },
  //园区名称
  village_name: function (e) {
    this.setData({
      village_name: e.detail.value
    })
  },
  //详细地址
  detailed_address: function (e) {
    this.setData({
      detailed_address: e.detail.value
    })
  },
 

  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var value_sel = that.data.value_sel;
    that.startAddressAnimation(false)
    // 将选择的城市信息放入数组后期方便操作
    var areaInfo_name = [];//区域名称
    areaInfo_name.push(that.data.province_s[value_sel[0]].name);
    areaInfo_name.push(that.data.city_s[value_sel[1]].name);
    areaInfo_name.push(that.data.area_s[value_sel[2]].name);
    var area_id_Info = [];//区域id
    area_id_Info.push(that.data.province_s[value_sel[0]].id);
    area_id_Info.push(that.data.city_s[value_sel[1]].id);
    area_id_Info.push(that.data.area_s[value_sel[2]].id);
    that.setData({
      areaInfo_name: areaInfo_name,
      area_id_Info: area_id_Info,
      price_name: areaInfo_name[0],
      city_name: areaInfo_name[1],
      area_name: areaInfo_name[2],
      province: area_id_Info[0],
      city: area_id_Info[1],
      area: area_id_Info[2],
    })
  },
  //点击黑色区域关闭地址选择
  hideCitySelected: function (e) {
    this.startAddressAnimation(false)
  },

  // 处理省市县联动逻辑
  cityChange: function (e) {
    var that = this;
    var value_sel = e.detail.value;//获取选中的id数组
    var province = this.data.province;
    var citys = this.data.citys
    var areas = this.data.areas
    var province_Num = value_sel[0]
    var city_Num = value_sel[1]
    var county_Num = value_sel[2]
    //判断是否需要切换市区级
    if (this.data.value_sel[0] != province_Num) {
      var provinceid = that.data.province_s[province_Num].id;//获取省id查询市级信息
      that.getCity(provinceid);//获取市级信息
      this.setData({
        value_sel: [province_Num, 0, 0],
      })
    } else if (this.data.value_sel[1] != city_Num) {//判断是否需要切换区域
      var cityid = that.data.city_s[city_Num].id;
      that.getArea(cityid);//获取区域信息
      this.setData({
        value_sel: [province_Num, city_Num, 0],
      })
    } else {
      this.setData({
        value_sel: [province_Num, city_Num, county_Num]
      })
    }
  },

  //获取省级信息 getProvince
  getProvince: function () {
    var that = this;
    app.ajax({
      url: 'Common/Common/getProvince',
      method: "POST",
      data: {},
      load:true,
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            province_s: res.data.data
          })
          //初次进入才会调用 获取市区级
          if (that.data.city_s.length == 0) {
            that.getCity(res.data.data[0].id);
          }
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },
  //获取市信息 getCity
  getCity: function (provinceid) {
    var that = this;
    app.ajax({
      url: 'Common/Common/getCity',
      method: "POST",
      data: { id: provinceid },
      load: true,
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            city_s: res.data.data
          });
          //重置区域信息 
          that.getArea(res.data.data[0].id);
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },
  //获取区域信息 getArea
  getArea: function (cityid) {
    var that = this;
    app.ajax({
      url: 'Common/Common/getArea',
      method: "POST",
      load: true,
      data: { id: cityid },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            area_s: res.data.data
          })
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },
  // .............................地址选择end

})